package com.weipai.education.service.impl;

import com.sun.security.auth.LdapPrincipal;
import com.weipai.education.dao.ActivityCateDao;
import com.weipai.education.dao.ActivityDao;
import com.weipai.education.enums.RetCode;
import com.weipai.education.model.ActivityCate;
import com.weipai.education.model.api.PageResult;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.ActivityCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/11 09:54
 */
@Service
public class ActivityCateServiceImpl implements ActivityCateService {
    
    @Autowired
    private ActivityCateDao activityCateDao;
    @Autowired
    private ActivityDao activityDao;

    @Override
    public Response list(Integer page, Integer size) {
        Response response = new Response();
        Integer start = (page-1) * size;
        List<Map<String, Object>> activity = activityCateDao.list(start, size);
        Integer totalNum = activityCateDao.listAmount();
        PageResult<Map<String, Object>> pageList = new PageResult<>(activity,page,size,totalNum);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    @Override
    public Response save(ActivityCate activityCate) {
        Response response = new Response();
        if(null !=activityCate.getId()){
            activityCateDao.update(activityCate);
        }else{
            activityCate.setStatus(0);
            activityCateDao.insert(activityCate);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response changeStatus(String id) {
        Response response = new Response();
        ActivityCate activityCate = activityCateDao.selectByPrimaryKey(id);
        if(null!=activityCate){
            if(0 ==activityCate.getStatus()){
                
                //更改停用，如果有此类别下的活动不能停用
                if(activityDao.getActivityByCateId(id)>0){
                    response.setCode(RetCode.CATE_HAVE_ACTIVITY_CHANGE);
                    return response;
                }
                
                activityCate.setStatus(-1);
            }else{
                activityCate.setStatus(0);
            }
            activityCateDao.update(activityCate);
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NOT_DATA);
        }
        return response;
    }

    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        
        //活动类别删除，如果有此类别下的活动不能删除
        for(String id1 : id){
            if(activityDao.getActivityByCateId(id1)>0){
                response.setCode(RetCode.CATE_HAVE_ACTIVITY_DELETE);
                return response;
            }
        }
        
        if(activityCateDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }
}
